package com.lishe.lishe.lishe.module.me.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.module.me.bean.UserListConfig;
import com.lishe.lishe.lishe.utils.AppUtils;
import com.lishe.lishe.lishe.utils.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhcasecom on 2017/8/15.
 */

public class MineAdapter extends BaseAdapter {
    private Context context;
    private List<UserListConfig> mineLists = new ArrayList<>();
    private int[] ImageArray = new int[]{R.mipmap.money_icon, R.mipmap.address_icon, R.mipmap.yijian_icon, R.mipmap.update_icon};

    public MineAdapter(Context context, List<UserListConfig> mineLists) {
        this.context = context;
        this.mineLists = mineLists;
    }

    public void resetData(List<UserListConfig> mineLists) {
        this.mineLists = mineLists;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mineLists == null ? 0 : mineLists.size();
    }

    @Override
    public Object getItem(int i) {
        return mineLists.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = View.inflate(context, R.layout.mine_list_item, null);
        }
        UserListConfig userListConfig = mineLists.get(i);
        TextView title = view.findViewById(R.id.type_txt);
        title.setText(userListConfig.getTitle());

        ImageView icon = view.findViewById(R.id.type_icon);
        icon.setImageResource(ImageArray[i]);
        ImageView url = view.findViewById(R.id.url);

        ImageView update_icon = view.findViewById(R.id.update_icon);
        TextView version = view.findViewById(R.id.version);
        update_icon.setVisibility(View.GONE);
        version.setVisibility(View.GONE);
        if (i == (mineLists.size() - 1)) {
            int app_code = SharedPreferencesUtils.getAppCode();
            if (app_code != 0 && app_code > AppUtils.getVersionCode(context)) {
                update_icon.setVisibility(View.VISIBLE);
                version.setVisibility(View.VISIBLE);
                String app_version = SharedPreferencesUtils.getAppVersion();
                if (!TextUtils.isEmpty(app_version))
                    version.setText("V" + app_version);
            }
        }

        return view;
    }
}
